package com.yanqu.road.dao.impl.activity.sonhai;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiUserData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiUserEggRewardData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SonHaiUserEggRewardDaoImpl extends TempDataObjectDao<SonHaiUserEggRewardData> {

    public static String getSonHaiUserEggRewardDataSql = "select * from `t_c_activity_son_hai_user_egg_reward_data` where `activityId`=? and groupId = ?";
    public Map<Long, Map<Integer, SonHaiUserEggRewardData>> getSonHaiUserEggRewardData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        //K:userId,K:奖励唯一ID
        Map<Long, Map<Integer, SonHaiUserEggRewardData>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSonHaiUserEggRewardDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                SonHaiUserEggRewardData data = parse(rs);
                Map<Integer, SonHaiUserEggRewardData> rewardMap = map.computeIfAbsent(data.getUserId(), k -> new ConcurrentHashMap<>());
                rewardMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateSonHaiUserEggRewardDataSql = "update `t_c_activity_son_hai_user_egg_reward_data` set eggId=?,reward=?,rewardFlag=?,rewardTime=?,createTime=? where activityId=? and groupId = ? and userId=? and id=?";
    @Override
    protected boolean updateData(SonHaiUserEggRewardData sonHaiUserEggRewardData) {
        sonHaiUserEggRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserEggRewardData.getEggId()));
        para.put(i++, new DbParameter(Types.VARCHAR, sonHaiUserEggRewardData.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserEggRewardData.getRewardFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserEggRewardData.getRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserEggRewardData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserEggRewardData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserEggRewardData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserEggRewardData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserEggRewardData.getId()));
        if (getDb().executeNoneQuery(updateSonHaiUserEggRewardDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSonHaiUserEggRewardDataSql = "insert into `t_c_activity_son_hai_user_egg_reward_data`(activityId,groupId,userId,id,eggId,reward,rewardFlag,rewardTime,createTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SonHaiUserEggRewardData sonHaiUserEggRewardData) {
        sonHaiUserEggRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserEggRewardData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserEggRewardData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserEggRewardData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserEggRewardData.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserEggRewardData.getEggId()));
        para.put(i++, new DbParameter(Types.VARCHAR, sonHaiUserEggRewardData.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserEggRewardData.getRewardFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserEggRewardData.getRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserEggRewardData.getCreateTime()));
        if (getDb().executeNoneQuery(insertSonHaiUserEggRewardDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SonHaiUserEggRewardData parseData(ResultSet rs) throws SQLException {
        SonHaiUserEggRewardData sonHaiUserEggRewardData = new SonHaiUserEggRewardData();
        sonHaiUserEggRewardData.setActivityId(rs.getInt("activityId"));
        sonHaiUserEggRewardData.setGroupId(rs.getInt("groupId"));
        sonHaiUserEggRewardData.setUserId(rs.getLong("userId"));
        sonHaiUserEggRewardData.setId(rs.getInt("id"));
        sonHaiUserEggRewardData.setEggId(rs.getInt("eggId"));
        sonHaiUserEggRewardData.setReward(rs.getString("reward"));
        sonHaiUserEggRewardData.setRewardFlag(rs.getInt("rewardFlag"));
        sonHaiUserEggRewardData.setRewardTime(rs.getLong("rewardTime"));
        sonHaiUserEggRewardData.setCreateTime(rs.getLong("createTime"));
        return sonHaiUserEggRewardData;
    }
}
